/**
 * Created by chenyong on 2015/11/15.
 */

var request = require("request");
var config = require('./../config');
var ServiceRequestError = require('./errors').ServiceRequestError;
var ServiceGeneralError = require('./errors').ServiceGeneralError;

var BigpixelSrv = {

    /**
     * 向chingkun service发送Get请求
     * @param url
     * @param form
     * @param callback
     * @constructor
     */
    GetData: function(url,form,callback){

        if (arguments.length === 2 && typeof form === 'function') {
            callback = form;
            form = null;
        }

        var access_token=config.api.token();
        url = config.api.rootpath + url;
        var reqinfo = {
            url: url,
            json:true,
            timeout:5000,
            headers: {'Authorization': 'Bearer ' + access_token}
        };

        if(form != null)
        {
            reqinfo.form = form;
        }

        request.get(reqinfo,function (error, response, data){
            if (!error && response.statusCode === 200)
            {
                callback(null,data);
            }
            else
            {
                var errmsg = "调用服务GET:" + url + " 时出错" + ((error && error.message) ? "，详细：" + error.message : "");
                var err = new ServiceRequestError(errmsg);
                callback(err);
            }
        });
    },

    /**
     * 向chingkun service发送Post请求
     * @param url
     * @param form
     * @param callback
     * @constructor
     */
    PostData: function(url,form,callback)
    {
        if (arguments.length === 2 && typeof form === 'function') {
            callback = form;
            form = null;
        }

        var access_token=config.api.token();
        url = config.api.rootpath + url;
        var options = {
            url: url,
            json:true,
            timeout:5000,
            headers: {'Authorization': 'Bearer ' + access_token}
        };

        if(form != null)
        {
            options.form = form;
        }

        request.post(options, function(error, response, data){

            if (!error && (response.statusCode === 200 || response.statusCode === 201))
            {
                callback(null,data);
            }
            else
            {
                var errmsg = "调用服务POST:" + url + " 时出错" + ((error && error.message) ? "，详细：" + error.message : "");
                var err = new ServiceRequestError(errmsg);
                callback(err);
            }
        });
    },

    /**
     * 向chingkun service发送Put请求
     * @param url
     * @param form
     * @param callback
     * @constructor
     */
    PutData: function(url,form,callback)
    {
        if (arguments.length === 2 && typeof form === 'function') {
            callback = form;
            form = null;
        }

        var access_token=config.api.token();
        url = config.api.rootpath + url;
        var options = {
            url: url,
            json:true,
            timeout:5000,
            headers: {'Authorization': 'Bearer ' + access_token}
        };

        if(form != null)
        {
            options.form = form;
        }

        request.put(options, function(error, response, data){
            if (!error && (response.statusCode === 200 || response.statusCode === 201))
            {
                callback(null,data);
            }
            else
            {
                var errmsg = "调用服务PUT:" + url + " 时出错" + ((error && error.message) ? "，详细：" + error.message : "");
                var err = new ServiceRequestError(errmsg);
                callback(err);
            }
        });
    },

    /**
     * 向chingkun service发送Delete请求
     * @param url
     * @param form
     * @param callback
     * @constructor
     */
    DelData: function(url,form,callback)
    {
        if (arguments.length === 2 && typeof form === 'function') {
            callback = form;
            form = null;
        }

        var access_token=config.api.token();
        url = config.api.rootpath + url;
        var options = {
            url: url,
            json:true,
            timeout:5000,
            headers: {'Authorization': 'Bearer ' + access_token}
        };

        if(form != null)
        {
            options.form = form;
        }

        request.del(options, function(error, response, data){
            if (!error && (response.statusCode === 200 || response.statusCode === 201))
            {
                callback(null,data);
            }
            else
            {
                var errmsg = "调用服务DEL:" + url + " 时出错" + ((error && error.message) ? "，详细：" + error.message : "");
                var err = new ServiceRequestError(errmsg);
                callback(err);
            }
        });
    },

    /**
     * 响应ttp请求，返回结果
     * @param res
     * @param statusCode
     */
    respondData: function(res, error, result, statusCode){
        if(error)
        {
            statusCode = statusCode || 500;
            res.status(statusCode).json(error);
        }
        else
        {
            statusCode = statusCode || 200;
            res.status(statusCode).json(result);
        }
    },

    /**
     * 响应http请求，返回一般性异常
     * @param res
     * @param err
     */
    handleGeneralError:function(res,err)
    {
        var error = new ServiceGeneralError(err);
        res.status(500).json(error);
    }
}

module.exports = BigpixelSrv;